<template>
  <div class="indexPage">
    <!-- 未登录部分 -->
    <div class="loginPage"
         v-if="showModel">
      <div class="model">
        <div class="modleContent">
          <div class="modelTitle">
            <img :src=info>
          </div>
          <div class="modelBody">微信登录需要获取您的用户信息，请前往设置</div>
          <div class="btns">
            <button open-type="getUserInfo"
                    class="agree"
                    lang="zh_CN"
                    @getuserinfo="getUser">{{setting}}</button>
          </div>
        </div>
      </div>
    </div>
    <!-- 登录部分显示首页 -->
    <div class="indexMain"
         v-else>
      <div class="topSelected">
        <p class="topCheck">
          查看全面成绩预测
        </p>
        <p class="topChoise"
           @click.stop="NavexamRomm">
          {{subject}}
          <span>
            <img :src=imageList.selectType
                 alt="">
          </span>
        </p>
      </div>
      <div class="topBg">
        <img :src=imageList.color
             alt=""
             class="bgImg">
        <div class="topOpition">
          <div class="listItem"
               @click.stop="acss">
            <img :src=imageList.option1
                 alt="">
            <span>课后练习</span>
          </div>
          <div class="listItem"
               @click.stop="navToTraning">
            <img :src=imageList.option2
                 alt="">
            <span>强化训练</span>
          </div>
          <div class="listItem"
               @click.stop="navTosimulation">
            <img :src=imageList.option3
                 alt="">
            <span>模拟考试</span>
          </div>
          <div class="listItem"
               @click.stop="navTorecord">
            <img :src=imageList.option4
                 alt="">
            <span>考试记录</span>
          </div>
          <div class="listItem"
               @click.stop="navToCollect">
            <img :src=imageList.option5
                 alt="">
            <span>习题收藏</span>
          </div>
          <div class="listItem"
               @click.stop="navToScore">
            <img :src=imageList.option6
                 alt="">
            <span>成绩单</span>
          </div>
        </div>
      </div>
      <div class="exam">
        <img :src=imageList.exam
             alt="">
        <span>新风国机微考堂</span>
      </div>
      <div class="examWrap">
        <div class="examList">
          <ul class="examListUl">
            <li @click.stop='navToFormal(item.id,item.name)'
                v-for="(item,index) in formalPaper"
                :key=index>
              <div class="liTop">
                <div class="leftHexagon">
                  <img :src=imageList.border
                       alt="">
                  <p class="examTitle">{{item.name}}</p>
                </div>
                <p class="play">
                  <img :src=imageList.study
                       alt="">
                </p>
              </div>
              <div class="ctTime">
                {{item.add_time}}
              </div>
            </li>

          </ul>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
import { formatTime } from '@/utils'
export default {
  data () {
    return {
      activeIndex: 1,
      subject: '中级人力资源',
      imageList: {
        selectType: 'https://dati.co-links.com/static/xadmin/images/selected.png',
        color: 'https://dati.co-links.com/static/xadmin/images/color.jpg',
        option1: 'https://dati.co-links.com/static/xadmin/images/option1.jpg',
        option2: 'https://dati.co-links.com/static/xadmin/images/option2.jpg',
        option3: 'https://dati.co-links.com/static/xadmin/images/option3.jpg',
        option4: 'https://dati.co-links.com/static/xadmin/images/option4.jpg',
        option5: 'https://dati.co-links.com/static/xadmin/images/option5.jpg',
        option6: 'https://dati.co-links.com/static/xadmin/images/option6.jpg',
        exam: 'https://dati.co-links.com/static/xadmin/images/exam.jpg',
        border: 'https://dati.co-links.com/static/xadmin/images/border.png',
        study: 'https://dati.co-links.com/static/xadmin/images/study.png'
      },
      formalPaper: [],
      disabled: false,
      initId: 0,
      show: true,
      text: '',
      telError: false,
      setting: '去设置',
      inputType: 'password',
      password: '',
      showPassword: false,
      passwordError: false,
      info: './../../static/images/info.jpg',
      showModel: false,   // 21:33  记得修改回
      totalArea: [],
      originData: -1
    }
  },
  computed: {
    ...mapState({
      currentExam: state => state.currentExam
    })
  },
  methods: {
    ...mapMutations(['setExamSubject', 'setExamPaper', 'setUserInfo']),
    checktel (str) {
      var reg2 = /^(((13|14|15|18|17)\d{9}))$/
      if (!reg2.test(str)) {
        return false
      }
      return true
    },
    checkTel () {
      if (!this.checktel(this.text)) {
        this.telError = true
      }
    },
    addActive () {
      if (this.activeIndex === 0) {
        this.activeIndex = 1
      } else {
        this.activeIndex = 0
      }
    },
    navToLogin () {
      wx.navigateTo({
        url: '../login/main'
      })
    },
    NavexamRomm () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../examinationRoom/main?id=' + (this.currentExam.currentId || this.initId)
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    acss () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../exercise/main'
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    navToTraning () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../hardTraining/main?type=2'
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    navTosimulation () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../examPaper/main?type=1' + '&roomId=' + (this.currentExam.roomIndex || 0)
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    navTorecord () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../examRecord/main'
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    navToCollect () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../collect/main'
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    navToScore () {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../score/main'
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    navToFormal (index, name) {
      if (wx.getStorageSync('openId')) {
        if (wx.getStorageSync('user_id')) {
          if (wx.getStorageSync('user_id') > 0) {
            this.showModel = false
            wx.navigateTo({
              url: '../exam/main?type=' + 'formal' + '&id=' + index + '&defaultItem=' + 0 + '&name=' + name
            })
            this.setExamPaper({
              paperName: name
            })
          }
        } else {
          this.showModel = true
        }
      } else {
        this.showModel = true
      }
    },
    init (index) {
      let that = this
      that.formalPaper = []
      console.log(that.$mp.query.id)
      this.originData = index
      this.$http.get('https://dati.co-links.com/exam/index/')
        .then(function (res) {
          if (res.data.code === '0000') {
            that.totalArea = res.data.data
            that.subject = res.data.data[index].name
            that.initId = res.data.data[index].id
            console.log('initId', that.initId)
            for (var i = 0; i < res.data.data.length; i++) {
              if (res.data.data[i].id === that.initId) {
                for (let k in res.data.data[i].papers) {
                  that.formalPaper.push(res.data.data[i].papers[k])
                }
              }
            }
            for (let k in that.formalPaper) {
              that.formalPaper[k].add_time = formatTime(that.formalPaper[k].add_time)
            }
          }
        })
    },
    getUser (e) {
      console.log(e.mp.detail.userInfo)
      var $this = this
      this.setting = '请稍后'
      wx.getSetting({
        success (res) {
          if (!res.authSetting['scope.userInfo']) {
            $this.showModel = true
            console.log('hasbenn authSetting ')
          } else {
            console.log('wx.loginIn')
            wx.login({
              success: (res) => {
                var code = res.code
                console.log(code)
                if (res.code) {
                  $this.userInfo = e.mp.detail.userInfo
                  console.log(e.mp.detail.userInfo)
                  $this.$http.post('https://dati.co-links.com/user/weixin_login/', {
                    code: code,
                    nickName: e.mp.detail.userInfo.nickName,
                    avatarUrl: e.mp.detail.userInfo.avatarUrl
                  }).then((res) => {
                    console.log(res.data.code)
                    if (res.data.code === '0000') {
                      console.log(res.data.data.openid)
                      $this.setUserInfo({
                        openid: res.data.data.openid,
                        nickName: e.mp.detail.userInfo.nickName,
                        avatarUrl: e.mp.detail.userInfo.avatarUrl
                      })
                      wx.setStorageSync('openId', res.data.data.openid)
                      wx.navigateTo({
                        url: '../login/main'
                      })
                    }
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  mounted () {
    if (wx.getStorageSync('openId') && wx.getStorageSync('user_id')) {
      this.showModel = false
    }
    this.init(0)
  },
  onShow () {
    console.log('onshow', this.currentExam.roomIndex)
    if (this.originData !== this.currentExam.roomIndex && this.currentExam.roomIndex !== undefined) {
      this.init(this.currentExam.roomIndex)
      console.log('changeIndex')
    }
    this.subject = this.currentExam.currentName || '国机党建'
  }
}
</script>
<style lang="stylus">
.indexMain {
  // background white
  height: 100%;
}

.indexPage {
  height: 100%;
}

.topSelected {
  height: 240rpx;
  background: linear-gradient(#00cc76, #1eda8b, #32e098);
  // background #00cc76
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  align-items: center;
  padding: 0 0 0 32rpx;
  justify-content: space-between;
  flex-wrap: wrap;

  .topCheck {
    color: white;
    font-size: 36rpx;
  }

  .topChoise {
    color: white;
    font-size: 28rpx;
    padding: 20rpx;
    border-top-left-radius: 33rpx;
    border-bottom-left-radius: 33rpx;
    background: rgba(0, 0, 0, 0.3);
    position: relative;

    span {
      padding-left: 20rpx;

      img {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }
}

.topBg {
  width: 100%;
  height: 444rpx;
  position: relative;

  .bgImg {
    width: 100%;
    height: 181rpx;
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
  }

  .topOpition {
    position: absolute;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    justify-content: space-around;
    flex-wrap: wrap;
    z-index: 1;
    height: 100%;
    background: white;
    left: 32rpx;
    right: 32rpx;
    padding-top: 55rpx;
    border-radius: 12rpx;
    -webkit-border-radius: 12rpx;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;

    .listItem {
      width: 33.33%;

      img {
        width: 100rpx;
        height: 100rpx;
        display: block;
        margin: 0 auto;
      }

      span {
        text-align: center;
        font-size: 30rpx;
        color: #333333;
        display: block;
        padding: 20rpx 0;
      }
    }
  }
}

.exam {
  padding: 0rpx 32rpx;
  height: 165rpx;
  position: relative;
  margin: 50rpx 0;

  img {
    width: 686rpx;
    height: 186rpx;
  }

  span {
    position: absolute;
    left: 120rpx;
    top: 32rpx;
    font-size: 36rpx;
    color: white;
    font-weight: bold;
  }

  &:after {
    position: absolute;
    left: 120rpx;
    top: 100rpx;
    content: '';
    width: 55rpx;
    height: 6rpx;
    background: white;
  }
}

.examListUl {
  margin: 0 32rpx;
  height: 300rpx;

  li {
    padding-top: 18rpx;
    padding-bottom: 0;
    border-bottom: 2rpx solid #e6e6e6;

    .liHide:nth-child(1) {
      padding-top: 0;
    }
  }
}

.liTop {
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  align-items: center;
  padding-bottom: 16rpx;

  .leftHexagon {
    width: 29rpx;
    // height 34rpx
    position: relative;
    flex: 1;

    img {
      width: 29rpx;
      height: 34rpx;
      position: absolute;
      margin-top: 10rpx;
      // margin-top 30rpx
    }

    .examTitle {
      font-size: 30rpx;
      color: #000000;
      padding-left: 56rpx;
      display: inline-block;
    }
  }
}

.play {
  width: 45rpx;
  height: 45rpx;
  float: right;

  img {
    width: 45rpx;
    height: 45rpx;
    margin-top: 30rpx;
  }
}

.ctTime {
  padding-bottom: 17rpx;
  border-left: 0;
  // margin-left 14rpx
  padding-left: 60rpx;
}

.liHide {
  font-size: 26rpx;
  color: #333333;
  position: relative;
  // margin-top 8rpx
  padding: 18rpx 0;
  padding-left: 60rpx;
  height: 32rpx;

  &:before {
    position: absolute;
    content: '';
    left: 8rpx;
    top: 50%;
    width: 16rpx;
    height: 16rpx;
    border-radius: 16rpx;
    background: #00cc76;
    z-index: 2;
  }

  &:after {
    position: absolute;
    content: '';
    left: 14rpx;
    top: calc(50% + 20rpx);
    width: 2rpx;
    height: 36rpx;
    background: #00cc76;
    z-index: 1;
  }
}

.subTitle {
  padding-left: 20rpx;
}

.subPlay {
  width: 32rpx;
  height: 32rpx;
  float: right;
  margin-right: 6rpx;

  img {
    width: 32rpx;
    height: 32rpx;
  }
}

.liLast {
  &:after {
    position: absolute;
    content: '';
    left: 14rpx;
    top: 20rpx;
    width: 0rpx;
    height: 36rpx;
    background: #00cc76;
  }
}

.hideExamContent {
  display: none;
}

li.active {
  padding-bottom: 30rpx;
}

li.active .hideExamContent {
  display: block;
}

li.active .ctTime {
  border-left: 2rpx solid #00cc76;
}

.model {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  background: white;
  z-index: 50;
}

.loginPage {
  width: 100%;
  height: 100%;
}

.loginWrap {
  width: 100%;
  height: 100%;
  /* background-image: url("../images/login.jpg"); */
  background-repeat: no-repeat;
  background-size: 100% 100%;
  position: relative;
}

.loginWrap .loginZoom, .loginWrap .fillPassword {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.loginTitle {
  font-size: 0.38rem;
  color: white;
  font-weight: bold;
  text-align: center;
  padding: 1.4rem 0;
  background: #00cc75;
  position: relative;
}

.loginTitle:after {
  position: absolute;
  bottom: 0;
  left: 50%;
  content: '';
}

.loginArea {
  margin: 0 0.76rem;
}

.loginInput {
  position: relative;
  height: 0.8rem;
}

#androidDialog1 {
  display: none;
}

.loginArea {
  margin-top: 0.65rem;
}

.loginArea input {
  width: 100%;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  padding-left: 0.3rem;
  height: 0.8rem;
  border-radius: 0.1rem;
  -webkit-border-radius: 0.1rem;
  font-size: 0.34rem;
  border: none;
  outline: none;
}

.loginInput input {
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 3px 5px #ccc;
}

::-webkit-input-placeholder { /* WebKit browsers */
  color: #999;
  font-size: 0.3rem;
}

.loginInput .errorTips {
  position: absolute;
  right: 0.6rem;
  top: 0;
  height: 100%;
  padding-right: 0.6rem;
  background-image: url('https://xcx.taosha.co/Uploads/images/errorTips.png');
  background-repeat: no-repeat;
  background-position: right center;
  background-size: 0.3rem 0.3rem;
  font-size: 0.24rem;
  color: #e83328;
  line-height: 0.8rem;
  z-index: 999;
  /* display: none; */
}

.loginInput .eyes {
  position: absolute;
  right: 0.3rem;
  top: 0;
  height: 100%;
  width: 0.68rem;
  background-image: url('https://xcx.taosha.co/Uploads/images/eyes2.png');
  background-size: 0.38rem 0.24rem;
  background-repeat: no-repeat;
  background-position: center;
  z-index: 999;
  /* display: none; */
}

.loginBtn {
  width: 100%;
  height: 0.8rem;
  margin-top: 0.3rem;
}

.loginBtn input {
  text-align: center;
  line-height: 0.8rem;
  font-size: 0.34rem;
  color: #ffffff;
  background: linear-gradient(to right, #04c373 0%, #00e999 100%);
  box-shadow: 0 3px 5px #ccc;
}

.loginTips {
  text-align: center;
  font-size: 0.24rem;
  color: #000;
  margin-top: 0.2rem;
}

// @keyframes shake {
// from, to {
// -webkit-transform: translate3d(0, 0, 0);
// transform: translate3d(0, 0, 0);
// }
// }
.shake {
  -webkit-animation-name: shake;
  animation-name: shake;
}

.animated {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}

.code {
  text-align: center;
  font-size: 0.24rem;
  color: white;
  padding-top: 0.13rem;
}

.sendCode {
  font-weight: normal;
}

.loginArea .codeArea {
  text-align: center;
}

.loginArea .codeArea input {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 0.13rem;
  background-color: rgba(255, 255, 255, 0.7);
  margin: 0 0.1rem;
}

.codeerrorTips {
  text-align: center;
  padding-top: 0.2rem;
  display: none;
}

.codeerrorTips span {
  display: inline-block;
  position: relative;
  font-size: 0.24rem;
  color: white;
  padding-left: 0.5rem;
}

.codeerrorTips span:before {
  position: absolute;
  content: '';
  left: 0;
  top: 50%;
  margin-top: -0.15rem;
  width: 0.3rem;
  height: 0.3rem;
  background-image: url('https://xcx.taosha.co/Uploads/images/errorTips.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size: 0.3rem 0.3rem;
}

.codeerrorTips span.noTips:before {
  width: 0;
}

.setPassword {
  display: none;
}

.fillPassword {
  /* display: none; */
}

.forgetPassword {
  font-size: 0.24rem;
  color: white;
  text-align: center;
  padding-top: 0.2rem;
}

.forgetPassword a {
  color: #000;
}

button[disabled] {
  color: white;
}

button[disabled]:not([type]) {
  color: rgba(255, 255, 255, 0.7);
  background-color: #48c4a0;
}
</style>
